package cn.sanch.product.service.impl;

import cn.sanch.basic.loginContext.LoginContext;
import cn.sanch.basic.service.IBaiduAiAuditService;
import cn.sanch.basic.util.BusinessException;
import cn.sanch.org.domain.Employee;
import cn.sanch.org.mapper.EmployeeMapper;
import cn.sanch.product.domain.Product;
import cn.sanch.product.domain.ProductDetail;
import cn.sanch.product.domain.ProductOnlineAuditLog;
import cn.sanch.product.mapper.ProductDetailMapper;
import cn.sanch.product.mapper.ProductMapper;
import cn.sanch.product.mapper.ProductOnlineAuditLogMapper;
import cn.sanch.product.service.IProductService;
import cn.sanch.basic.service.impl.BaseServiceImpl;
import cn.sanch.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Override
    public void save(Product product) {
        // 先添加服务对象，这样才会有自增长id
        super.save(product);
        // 获取明细对象
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            productDetailMapper.save(productDetail);
        }
    }

    @Override
    public void update(Product product) {
        super.update(product);
        // 获取明细对象
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null) {
            productDetail.setProductId(product.getId());
            // 调用方法根据服务id修改服务明细对象
            productDetailMapper.updateByProductId(productDetail);
        }
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
        // 调用方法根据服务id删除服务明细对象
        productDetailMapper.deleteByProductId(id);
    }

    /**
     * 服务批量上架
     * @param ids
     * @param request
     */
    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        // 1.调用工具类获取当前登录人的信息
        Logininfo logininfo = LoginContext.getLogininfo(request);

        // 2.通过logininfo获取审核人
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());

        // 3.遍历ids
        for (Long id : ids) {
            Product product = productMapper.findOne(id);

            Boolean textBoo = baiduAiAuditService.textAudit(product.getName());
            Boolean imgBoo = baiduAiAuditService.imageAudit(product.getResources());

            if (textBoo && imgBoo) {

                // 修改服务对象的状态和上架时间
                product.setState(1L);
                product.setOnsaletime(new Date());

                productMapper.update(product);

                // 添加审核通过日志对象
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setState(1);
                log.setAuditTime(new Date());
                log.setAuditId(employee.getId());
                log.setNote("服务上架审核成功！");

                productOnlineAuditLogMapper.save(log);

            }else {
                // 添加审核不通过日志对象
                ProductOnlineAuditLog log = new ProductOnlineAuditLog();
                log.setProductId(product.getId());
                log.setState(-1);
                log.setAuditTime(new Date());
                log.setAuditId(employee.getId());
                log.setNote("服务上架审核失败，名字或图片违规！");

                productOnlineAuditLogMapper.save(log);

                throw new BusinessException("服务上架审核失败，名字或图片违规！");
            }
        }
    }

    /**
     * 服务批量下架
     * @param ids
     */
    @Override
    public void offsale(Long[] ids) {
        Date offsaletime = new Date();
        productMapper.offsale(ids, offsaletime);


    }
}
